// pages/main/index.js
Page({
  data: {
    nickName: wx.getStorageSync('nickName'),
    headPortrait: wx.getStorageSync('headPortrait'),
    schoolName: wx.getStorageSync('schoolName'),
    id:null
  },
  mypost(e) {
    console.log(e)
    wx.navigateTo({
      url: '/pages/mypost/index',
    })
  },
  mycollection(e) {
    wx.navigateTo({
      url: '/pages/mycomment/index',
    })
  },
  mycomment(e) {
    wx.navigateTo({
      url: '/pages/mycollection/index',
    })
  },
  subject(e) {
    wx.navigateTo({
      url: '/pages/subject/index',
    })
  },
  school(e) {
    wx.navigateTo({
      url: '/pages/school/index',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    let openid = wx.getStorageSync('openid');
    let url = wx.getStorageSync('url')
    that.setData({
      nickName: wx.getStorageSync('nickName'),
      headPortrait: wx.getStorageSync('headPortrait'),
      schoolName: wx.getStorageSync('schoolName'),
    })
    wx.getSetting({
      success: res => {
        console.log(res)
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success: res => {
              if (res.userInfo.avatarUrl != wx.getStorageSync('headPortrait'))
                wx.request({
                  url: url + "/v1/user/head_portrait",
                  data: {
                    headPortrait: res.userInfo.avatarUrl
                  },
                  method: "put",
                  header: {
                    'content-type': 'application/json',
                    'authorization': wx.getStorageSync('authorization')
                  },
                  success: function (res) {
                    wx.setStorageSync('headPortrait', res.userInfo.avatarUrl);
                  }
                })
              if (res.userInfo.nickName != wx.getStorageSync('nickName'))
                wx.request({
                  url: url + "/v1/user/nick_name",
                  data: {
                    nickName: res.userInfo.nickName
                  },
                  method: "put",
                  header: {
                    'content-type': 'application/json',
                    'authorization': wx.getStorageSync('authorization')
                  },
                  success: function (res) {
                    wx.setStorageSync('nickName', res.userInfo.nickName);
                  }
                })
              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况

              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)

              }
            }
          })
        } else {
          wx.reLaunch({
            url: '/pages/login/index',
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})